//
//  QYGHomeViewController.m
//  wawaji_ios
//
//  Created by 奇艺果 on 2018/1/5.
//  Copyright © 2018年 yiguo qi. All rights reserved.
//

#import "QYGHomeViewController.h"
#import "CMMainCell.h"
#import "QYGUserInfoModel.h"
#import "SDCycleScrollView.h"
#import "CMCatchDollViewController.h"
#import "QYGNetRequestModel.h"
#import "QYGConfigModel.h"
#import "QYGMainCellFrameModel.h"
#import "LCScrollAttributeView.h"


static NSString *CMMainCellID = @"CMMainCellID";
static NSString *CMMainHeaderIdentifier = @"CMMainHeaderIdentifier";
static NSString *CMMainFooterIdentifier = @"CMMainFooterIdentifier";

@interface QYGHomeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSArray *banners;
@property (nonatomic, strong) LCScrollAttributeView *scrollAttriView;
@property (nonatomic, strong) NSArray *noticeArray;
@property (nonatomic, strong) UILabel *noMoreDataTipView;
@end

@implementation QYGHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = [QYGKeychain appName];
    [self setupUI];
    [self setupData];
}

- (void)setupData{
    [self loadNewData];
    [self loadBanners];
}
- (void)setUserInfo:(QYGUserInfoModel *)userInfo{
    _userInfo = userInfo;
}
#pragma mark setUI
- (void)setupUI{
    [self.view addSubview:self.collectionView];
    WS(weakSelf);
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
}

#pragma mark - <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if(self.noticeArray.count){
        return CGSizeMake(0, SCREEN_WIDTH *0.43 + ScaleW(40));
    }else{
        return CGSizeMake(0, SCREEN_WIDTH *0.43);
    }
}
#pragma mark - <UICollectionViewDataSource>

//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                            withReuseIdentifier:CMMainHeaderIdentifier
                                                                                   forIndexPath:indexPath];
        [headView addSubview:self.cycleScrollView];
        if(self.noticeArray.count){
            [headView addSubview:self.scrollAttriView];
        }
        return headView;
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                                                withReuseIdentifier:CMMainFooterIdentifier
                                                                                       forIndexPath:indexPath];
        [footerView addSubview:self.noMoreDataTipView];
        return footerView;
    }else{
        return nil;
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CMMainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CMMainCellID forIndexPath:indexPath];
    cell.mainCellFModel = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    QYGMainCellFrameModel *mainCellFModel = self.dataArray[indexPath.row];
    CMCatchDollViewController *vc = [[CMCatchDollViewController alloc] initWithModel:mainCellFModel.dollListModel];
    vc.userInfo = self.userInfo;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - loadData
// 获取用户信息
- (void)getUserInfoData{
    [QYGNetRequestModel getUserInfoCompleted:^(QYGUserInfoModel *userInfo) {
        self.userInfo = userInfo;
    } WithErrorCodeBlock:^(NSError *error) {
    }];
}
//获取头部轮播图
- (void)loadBanners{
    [QYGNetRequestModel getBannersCompleted:^(NSArray *bannerArr) {
        self.banners = bannerArr;
        self.cycleScrollView.imageURLStringsGroup = bannerArr;
    } WithErrorCodeBlock:^(NSError *error) {
    }];
}

- (void)loadMoreData{
    [self loadDataWithPage:self.page + 1];
}
- (void)loadNewData {
    [self loadDataWithPage:1];
    if(!self.banners.count){
        [self loadBanners];
    }
    [self loadNoticeData];
}
- (void)loadNoticeData{
    [QYGNetRequestModel getNoticeListDataCompleted:^(NSArray *noticeArr) {
        self.noticeArray = noticeArr;
        self.scrollAttriView.dataArray = noticeArr;
        [self.collectionView reloadData];
    } WithErrorCodeBlock:^(NSError *error) {
        
    }];
}
- (void)loadDataWithPage:(NSInteger)page{
    NSInteger per_page = 20;
    [SVProgressHUD show];
    [QYGNetRequestModel getDrollListDataWithPage:page per_page:per_page completed:^(NSArray *noticeEntityArr) {
        [SVProgressHUD dismiss];
        [self endRefresh];
        UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
        if(noticeEntityArr.count < per_page){
            flowLayout.footerReferenceSize = CGSizeMake(0, ScaleW(80));
            self.noMoreDataTipView.hidden = NO;
        }else{
            flowLayout.footerReferenceSize = CGSizeMake(0, ScaleW(30));
            self.noMoreDataTipView.hidden = YES;
        }
        if(page == 1) {self.dataArray = [NSMutableArray array];}
        [self.dataArray addObjectsFromArray:noticeEntityArr];
        [self.collectionView reloadData];
        self.page = page;
    } WithErrorCodeBlock:^(NSError *error) {
        [self endRefresh];
    }];
}

- (void)endRefresh{
    [self.collectionView.mj_header endRefreshing];
    [self.collectionView.mj_footer endRefreshing];
}

#pragma mark - 获取配置信息
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.collectionView.frame = self.view.bounds;
}
#pragma mark - lazy
- (UICollectionViewFlowLayout *)flowLayout
{
    if(!_flowLayout)
    {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(homFlowWith, homFlowWith * 1.21);
        _flowLayout.sectionInset = UIEdgeInsetsMake(homMargin, homMargin, homMargin, homMargin);
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout.footerReferenceSize = CGSizeMake(0, ScaleW(30));
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView
{
    if(!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor colorWithHex:@"ffffff"];
        [_collectionView registerClass:[CMMainCell class] forCellWithReuseIdentifier:CMMainCellID];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CMMainHeaderIdentifier];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CMMainFooterIdentifier];
    }
    return _collectionView;
}
- (SDCycleScrollView *)cycleScrollView{
    if(!_cycleScrollView){
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_WIDTH *0.43) delegate:nil placeholderImage:[UIImage imageNamed:@"placeholder"]];
//        [_cycleScrollView viewWithcornerRadius:10 borderWidth:0 borderColor:nil backgroundColor:nil];
        _cycleScrollView.currentPageDotColor = [UIColor whiteColor];
    }
    return _cycleScrollView;
}

- (LCScrollAttributeView *)scrollAttriView{
    if(!_scrollAttriView){
        _scrollAttriView = [[LCScrollAttributeView alloc] initWithFrame:CGRectMake(0, SCREEN_WIDTH *0.43  , SCREEN_WIDTH, ScaleW(40))];
    }
    return _scrollAttriView;
}

- (UILabel *)noMoreDataTipView{
    if(!_noMoreDataTipView){
        _noMoreDataTipView = [UILabel labelWithText:@"~到底啦，别拉啦～" setFont:ScaleFont(12) setTextColor:[UIColor colorWithHex:@"999999"]];
        _noMoreDataTipView.frame = CGRectMake(0, ScaleW(30), SCREEN_WIDTH, ScaleW(20));
    }
    return _noMoreDataTipView;
}
@end
